// 被观察者
class Subject {
    constructor(name) {
        this.name = name;
        this.state = 'happy';
        this.observers = [];
    }
    // 被观察者要收集观察者，后续状态改变，通知观察者
    attach(o) {
        this.observers.push(o);
    }
    setState(newState) {
        this.state = newState;
        this.observers.forEach(o => o.update(this.name, newState));
    }
    
}
// 观察者
class Observer {
    constructor(name) {
        this.name = name;
    }
    update(baby, state) {
        console.log(this.name + ',' + baby + '现在' + state);
    }
}

let baby = new Subject('xiaoming');
let dad = new Observer('dad');
let mam = new Observer('mom');

baby.attach(dad);
baby.attach(mam);
baby.setState('crying');